#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
@author: jiu
@contact: 643562684@qq.com
@time: 2022/3/14 
"""
import time

from Conf.setting import *
from browser.chrome import CHROME
from common.log import logger
from common.create_data import dt
from common.read_Yaml import Read_Yaml
from common.read_Ini import Read_InI
from PageProjects.base_page import BasePage


class CommonPage(BasePage):
    BasePage.element_data.update(
        Read_Yaml(YAML_FILE_LIST["com_page"]).data
    )
    _dt = dt

    _url = PROJECT_URL_HOME_PAGE

    # 基本信息 账号, 密码, 万能验证码
    user_info = Read_InI(PROJECT_CONF_SETTING['project']).data["common"]


    def __getattr__(self, *loc):
        try:
            if loc[0] not in self.element_data.keys():
                logger.exception(f"查找的元素不存在 :{loc[0]}")
                raise ValueError

            # Non eight point element location method
            if self.element_data[loc[0]][0] not in ELEMENT_POS:
                logger.exception(f"元素查找的类型不是常见的类型 xpath,id, ...: {self.element_data[loc[0]][0]}")
                raise TypeError

            return self.element_data.get(loc[0])
        except Exception as e:
            logger.exception(f"元素定位失败 ： {loc}:  失败元素的值是： {self.element_data[loc[0]]}")
            raise e

    def login(self, username=None, password=None):
        """登入模块"""
        self.open_url(self._url)
        self.el_click(self.home_page_login, page_action="点击登入按钮")
        self.element_input(self.login_username, value=username or self.user_info["username"],
                           page_action=f"定位用户输入框输入: {username or self.user_info['username']}")
        self.element_input(self.login_pwssword, value=password or self.user_info["password"],
                           page_action=f"定位密码输入框输入: {password or self.user_info['password']}")
        self.wait_element_visible(self.login_Jverify_img,page_action="等待验证先加载在输入")
        self.element_input(self.login_Jverify, value=self.user_info["verify_code"],
                           page_action=f"定位验证码输入: {self.user_info['verify_code']}")
        self.el_click(self.login_button, page_action="点击登入按钮")
        self.el_click(self.confirm_login, page_action="点击确定登入")

    def logout(self):
        """退出按钮"""
        self.el_click(self.home_page_logout_username, page_action="点击用户名")
        self.el_click(self.home_page_logout, page_action="点击退出按钮")

    def register(self, vip_type=None):
        """注册模块"""
        self.open_url(self._url)
        self.el_click(self.home_page_register, page_action="点击首页的注册按钮按钮")
        if not vip_type:
            self.el_click(self.register_ordinary_vip, page_action="选择注册会员类型为普通会员")
        else:
            self.el_click(self.register_enterprise_vip, page_action="选择注册会员类型为普通会员")
        nick_name = self._dt.random_name
        self.other_authentication['register'].append(nick_name)
        self.element_input(self.register_user_name, value=nick_name, page_action="注册界面,用户输入用户昵称")
        self.element_input(self.register_phone_number, value=self._dt.random_phone_number,
                           page_action="注册界面,用户输入手机号码")
        self.element_input(self.register_password, value=self.user_info['password'], page_action="注册界面,输入密码")
        self.element_input(self.register_password_confirm, value=self.user_info['password'],
                           page_action="注册界面,确认输入密码")
        self.element_input(self.register_ems, value=self._dt.random_phone_number, page_action="注册界面,输入EMS")
        self.element_input(self.register_home_address, value=self._dt.random_address, page_action="注册界面,输入家庭地址")
        self.element_input(self.register_verify_code, value=self.user_info['verify_code'], page_action="注册界面,输入验证码")
        self.element_input(self.register_settings_referer, value=self.user_info["username"],
                           page_action="注册界面,输入推荐人")
        # self.el_click(self.register_j_agreement, page_action="注册界面,点击同意协议")
        self.el_click(self.register_button, page_action="注册界面,点击注册按钮")
        self.el_click(self.login_sikp_setting, page_action="点击以后设置")


